package com.example.homescreen.model;
import java.util.ArrayList;

import com.example.homescreen.R;
import com.example.homescreen.entity.Food;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class FoodImageAdapter extends ArrayAdapter<Food> {
	Context context;
	int layoutResourceId;
	ArrayList<Food> data = new ArrayList<Food>();
	public Resources res;

	public FoodImageAdapter(Context context, int layoutResourceId,
			ArrayList<Food> data, Resources resLocal) {
		super(context, layoutResourceId, data);
		this.layoutResourceId = layoutResourceId;
		this.context = context;
		this.data = data;
		res = resLocal;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View row = convertView;
		ImageHolder holder = null;
		if (row == null) {
			LayoutInflater inflater = ((Activity) context).getLayoutInflater();
			row = inflater.inflate(layoutResourceId, parent, false);
			holder = new ImageHolder();
			holder.txtTitle = (TextView) row.findViewById(R.id.product_name);
			holder.imgIcon = (ImageView) row.findViewById(R.id.list_image);
			row.setTag(holder);
		} else {
			holder = (ImageHolder) row.getTag();
		}
		Food picture = data.get(position);
		holder.txtTitle.setText(picture.getfName());
		holder.imgIcon.setImageResource(res.getIdentifier(
				"com.example.homescreen:drawable/" + picture.getfImage(), null,
				null));
		return row;
	}

	static class ImageHolder {
		ImageView imgIcon;
		TextView txtTitle;
	}
}
